package com.im.partchat.ui.activity.conversation;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.im.partchat.R;
import com.im.partchat.base.BaseActivity;
import com.im.partchat.utils.OperationRong;
import com.im.partchat.widget.dialog.CommomDialog;
import com.im.partchat.widget.switchbutton.SwitchButton;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

public class SingleSettingActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.friend_header)
    ImageView friendHeader;
    @BindView(R.id.friend_name)
    TextView friendName;
    @BindView(R.id.group_info)
    LinearLayout groupInfo;
    @BindView(R.id.sw_friend_notfaction)
    SwitchButton swFriendNotfaction;
    @BindView(R.id.sw_message_top)
    SwitchButton swMessageTop;
    @BindView(R.id.clean_friend)
    LinearLayout cleanFriend;

    private UserInfo mUserInfo;
    private Conversation.ConversationType mConversationType;
    private String fromConversationId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_single_setting;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        fromConversationId = getIntent().getStringExtra("TargetId");
        mConversationType = (Conversation.ConversationType) getIntent().getSerializableExtra("conversationType");
        swFriendNotfaction.setOnCheckedChangeListener(this);
        swMessageTop.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {

        if (!TextUtils.isEmpty(fromConversationId)) {
            mUserInfo = RongUserInfoManager.getInstance().getUserInfo(fromConversationId);
            updateUI();
        }
    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {

    }

    @OnClick({R.id.friend_header, R.id.clean_friend})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.friend_header:

                break;
            case R.id.clean_friend:
                new CommomDialog(SingleSettingActivity.this).setTitle("确定清除聊天记录吗？").setPositiveOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        {
                            if (mUserInfo != null) {
                                RongIM.getInstance().clearMessages(Conversation.ConversationType.PRIVATE, mUserInfo.getUserId(), new RongIMClient.ResultCallback<Boolean>() {
                                    @Override
                                    public void onSuccess(Boolean aBoolean) {
                                        showToast(getString(R.string.clear_success));
                                    }

                                    @Override
                                    public void onError(RongIMClient.ErrorCode errorCode) {
                                        showToast(getString(R.string.clear_failure));
                                    }
                                });
                            }
                        }
                    }
                }).show();
                break;
        }
    }

    private void updateUI() {
        if (mUserInfo != null) {
            if (mUserInfo != null) {
                friendName.setText(mUserInfo.getName());
                Glide.with(SingleSettingActivity.this)
                        .load(mUserInfo.getPortraitUri())
                        .centerCrop()
                        .crossFade()
                        .into(friendHeader);

            }
            getState(mUserInfo.getUserId());
        }
    }

    private void getState(String targetId) {
        if (targetId != null) {//群组列表 page 进入
            if (RongIM.getInstance() != null) {
                RongIM.getInstance().getConversation(Conversation.ConversationType.PRIVATE, targetId, new RongIMClient.ResultCallback<Conversation>() {
                    @Override
                    public void onSuccess(Conversation conversation) {
                        if (conversation == null) {
                            return;
                        }

                        if (conversation.isTop()) {
                            swMessageTop.setChecked(true);
                        } else {
                            swMessageTop.setChecked(false);
                        }

                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode errorCode) {

                    }
                });

                RongIM.getInstance().getConversationNotificationStatus(Conversation.ConversationType.PRIVATE, targetId, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
                    @Override
                    public void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {

                        if (conversationNotificationStatus == Conversation.ConversationNotificationStatus.DO_NOT_DISTURB) {
                            swFriendNotfaction.setChecked(true);
                        } else {
                            swFriendNotfaction.setChecked(false);
                        }
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode errorCode) {

                    }
                });
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.sw_friend_notfaction:
                if (isChecked) {
                    if (mUserInfo != null) {
                        OperationRong.setConverstionNotif(Conversation.ConversationType.PRIVATE, mUserInfo.getUserId(), true);
                    }
                } else {
                    if (mUserInfo != null) {
                        OperationRong.setConverstionNotif(Conversation.ConversationType.PRIVATE, mUserInfo.getUserId(), false);
                    }
                }
                break;
            case R.id.sw_message_top:
                if (isChecked) {
                    if (mUserInfo != null) {
                        OperationRong.setConversationTop(Conversation.ConversationType.PRIVATE, mUserInfo.getUserId(), true);
                    }
                } else {
                    if (mUserInfo != null) {
                        OperationRong.setConversationTop(Conversation.ConversationType.PRIVATE, mUserInfo.getUserId(), false);
                    }
                }
                break;
        }
    }
}
